package OutputStream;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-09-29
 * Time: 21:26
 */
public class FileOutputStreamTest2 {

    //在写入数据的时候.如果文件不存在会自动创建新的文件.
    //每次写入新的数据会覆盖掉原来的数据,如果想要追加的写入数据就需要在构造方法中再加一个参数true
    public static void FileOutputStreamDemo1() throws IOException {
        //使用write一个一个的写入数据
        try(OutputStream outputStream = new FileOutputStream("e://hello.txt")){
            outputStream.write('h');
            outputStream.write('e');
            outputStream.write('l');
            outputStream.write('l');
            outputStream.write('o');
            outputStream.flush();//刷新缓冲区
        }
    }

    //写入字节数组中的数据 write(byte[])
    public static void FileOutputStreamDemo2() throws IOException {
        try(OutputStream outputStream = new FileOutputStream("e://world.txt")){
            byte[] bytes = {'w','o','r','l','d'};
            outputStream.write(bytes);
            outputStream.flush();//刷新缓冲区
        }
    }

    public static void FileOutputStreamDemo3() throws IOException {
        try(OutputStream outputStream = new FileOutputStream("e://world.txt",true)){
            String str = "world";
            outputStream.write(str.getBytes());
            outputStream.flush();//刷新缓冲区
        }
    }


    public static void main(String[] args) throws IOException {
        FileOutputStreamDemo3();
    }

}
